package com.farm.farming.controller;


import com.farm.entity.PageResult;
import com.farm.entity.Result;
import com.farm.entity.StatusCode;
import com.farm.farming.pojo.Cust_role;
import com.farm.farming.service.Cust_roleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/custRole")
@RestController
public class Cust_roleController {

    @Autowired
    private Cust_roleService cust_roleService;

    @PostMapping("/add")
    public Result add(@RequestBody Cust_role cust_role){

        System.out.println("--------");
        try {
            cust_roleService.add(cust_role,cust_role.getMenuIds(),cust_role.getPermissionIds());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"添加角色失败");
        }
        return new Result(true,StatusCode.OK,"添加角色完成");
    }

    @GetMapping("/deleteById")
    public Result deleteById(@RequestParam("id")Integer id){

            cust_roleService.delete(id);

        return new Result(true,StatusCode.OK,"删除角色完成");
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Cust_role cust_role){

        cust_roleService.update(cust_role);

        return new Result(true,StatusCode.OK,"修改角色完成");
    }

    @GetMapping("/findByid")
    public Result findByid(@RequestParam("id")Integer id){

        Cust_role byId = cust_roleService.findById(id);

        return new Result(true,StatusCode.OK,"查询单个角色完成",byId);
    }

    @PostMapping("findPage")
    public Result findPage(@RequestBody Cust_role cust_role,
                           @RequestParam(value = "Page",required = false,defaultValue = "0")Integer currentPage,
                           @RequestParam(value = "size",required = false,defaultValue = "10")Integer size){

        PageResult<Cust_role> page = cust_roleService.findPage(cust_role,currentPage,size);

        return new Result(true,StatusCode.OK,"分页查询完成",page);
    }

    @GetMapping("findAll")
    public Result findAll(){

        List<Cust_role> list = cust_roleService.findAll();

        return new Result(true,StatusCode.OK,"查询完成",list);
    }

}
